﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nop.Core;
using Nop.Services.Localization;

namespace Nop.Services.Customers
{
    public class MerchantApplyVerifylService : IMerchantApplyVerifylService
    {
        private readonly IMerchantApplyService _merappService;
        private readonly ILocalizationService _localizationService;


        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="customerService">Customer service</param>
        /// <param name="encryptionService">Encryption service</param>
        /// <param name="newsLetterSubscriptionService">Newsletter subscription service</param>
        /// <param name="localizationService">Localization service</param>
        /// <param name="rewardPointsSettings">Reward points settings</param>
        /// <param name="customerSettings">Customer settings</param>
        public MerchantApplyVerifylService(IMerchantApplyService merchantApplyServic,
            ILocalizationService localizationService)
        {
            this._merappService = merchantApplyServic;
            this._localizationService = localizationService;
        }

        #endregion

        /// <summary>
        /// Register customer
        /// </summary>
        /// <param name="request">Request</param>
        /// <returns>Result</returns>
        public virtual MerchantApplyVerifyResult RegisterMerchant(MerchantApplyVerifylRequest request)
        {
            if (request == null)
                throw new ArgumentNullException("request");

            if (request.MerApp == null)
                throw new ArgumentException("Can't load current customer");

            var result = new MerchantApplyVerifyResult();

            if (String.IsNullOrEmpty(request.Name))
            {
                result.AddError(_localizationService.GetResource("MerApp.Register.Errors.NameIsNotProvided"));
                return result;
            }

            if (String.IsNullOrEmpty(request.Phone))
            {
                result.AddError(_localizationService.GetResource("MerApp.Register.Errors.PhoneIsNotProvided"));
                return result;
            }

            if (!CommonHelper.IsValidPhone(request.Phone))
            {
                result.AddError(_localizationService.GetResource("Common.WrongPhone"));
                return result;
            }
            

            //at this point request is valid
            request.MerApp.Name = request.Name;
            request.MerApp.Phone = request.Phone;

            _merappService.InsertMerApp(request.MerApp);
            return result;
        }
    }
}
